#define _CRT_SECURE_NO_WARNINGS 1
#include <initializer_list>
#include <stack>
#include <string>
#include <iostream>
using namespace std;
using namespace std;
class Solution {
public:
    stack<int> nums;
    stack<char> ops;
    bool PerCal(char a, char b)
    {
        if (a == b) return true;
        if (b == '+' || b == '-')
        {
            if (a == '*')
            {
                return true;
            }
        }
        return false;
    }
    int operation(int a, int b, char op)
    {
        if (op == '+') return a + b;
        if (op == '-') return a - b;
        if (op == '*') return a * b;
        return '-1';
    }
    // bool IsOp(char c)
    // {
    //     if(c=='+'||c=='-'||c=='*') return true;
    //     return false;
    // }
    void insert_op(string number, char op)
    {
        cerr << "number=" << number << ",op=" << op << endl;
        if (op == '(')
        {
            ops.push(op);
        }
        else if (op == ')')
        {
            if (!number.empty()) nums.push(stoi(number));
            while (!ops.empty())
            {
                char oper = ops.top();
                ops.pop();
                if (oper == '(') break;
                int a = nums.top();
                nums.pop();
                int b = nums.top();
                nums.pop();
                int result = operation(b, a, oper);
                cerr << "result=" << result << endl;
                nums.push(result);
            }
        }
        else if (op == '#')
        {
            if (!number.empty())nums.push(stoi(number));
            while (!ops.empty())
            {
                char oper = ops.top();
                ops.pop();
                int a = nums.top();
                nums.pop();
                int b = nums.top();
                nums.pop();
                int result = operation(b, a, oper);
                cerr << "result=" << result << endl;
                nums.push(result);
            }
        }
        else
        {
            if (!number.empty()) nums.push(stoi(number));
            while (!ops.empty() && ops.top() != '(' && PerCal(ops.top(), op))
            {
                char stop = ops.top();
                ops.pop();
                int a = nums.top();
                nums.pop();
                int b = nums.top();
                nums.pop();
                int result = operation(b, a, stop);
                cerr << "result=" << result << endl;
                nums.push(result);
            }
            ops.push(op);
        }
    }
    int solve(string s)
    {
        s += '#';
        string number;
        for (int i = 0;i < s.size();++i)
        {
            if (s[i] >= '0' && s[i] <= '9')
            {
                number += s[i];
            }
            else
            {
                insert_op(number, s[i]);
                number.clear();
            }
        }
        return nums.top();
    }
};